﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingManageSystem.BLL;
using TeachingManageSystem.Entity;

namespace TeachingManageSystem.Web
{
    public partial class DetailOrEditBook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetBookDetailorEditBook();
            }  
        }
        /// <summary>
        /// 根据BookNumber获取教材的基本信息
        /// </summary>
        public void GetBookDetailorEditBook()
        {
            TbxBookNumber.Enabled = false;
            TbxBookName.Enabled = false;
            TbxBookAuthor.Enabled = false;
            TbxBookPrice.Enabled = false;
            TbxBookPublished.Enabled = false;
            TbxBookPublishedTime.Enabled = false;
            TbxRemark.Enabled = false;
            DrpBookType.Enabled = false;
            BtnEdit.Visible = false;
            FileUpload1.Visible = false;
            Button1.Visible = false;
            BtnExc.Visible = false;
            BtnEdited.Visible = true;
            var b = new Book
            {
                Number = Request.QueryString["booknumber"]//获取教材编号
            };
            var t = BookBll.GetBookByBookNym(b).Tables[0];
            TbxBookNumber.Text = t.Rows[0][0] as string;
            TbxBookName.Text = t.Rows[0][1] as string;
            TbxBookPublished.Text = t.Rows[0][2] as string;
            TbxBookAuthor.Text = t.Rows[0][4] as string;
            TbxBookPublishedTime.Text = Convert.ToDateTime(t.Rows[0][3]).ToShortDateString();
            TbxBookPrice.Text = Convert.ToDecimal(t.Rows[0][5]).ToString(CultureInfo.InvariantCulture);
            DrpBookType.DataSource = BookTypeBll.GetBookType();
            DrpBookType.DataBind();
            DrpBookType.SelectedValue = t.Rows[0][6].ToString();
            TbxRemark.Text = t.Rows[0][9].ToString();
            var imgid = Request.QueryString["ImageID"];
            img.Src = "ShowImage.aspx?ImageID=" + imgid;
        }

        protected void BtnEditClick(object sender, EventArgs e)
        {
            var book = new Book
            {
                Number = TbxBookNumber.Text,
                Name = TbxBookName.Text,
                Publisher = TbxBookPublished.Text,
                PublistedTime = Convert.ToDateTime(TbxBookPublishedTime.Text),
                Author = TbxBookAuthor.Text,
                Price = Convert.ToDecimal(TbxBookPrice.Text),
                BookTypeID = Convert.ToInt32(DrpBookType.SelectedValue),
                Remark = TbxRemark.Text
            };
            BookBll.UpdateBookByBooknumber(book);
            Response.Redirect("ShowBook.aspx");
        }
        /// <summary>
        /// 取消修改重新加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnExcClick(object sender, EventArgs e)
        {
            GetBookDetailorEditBook(); //解锁
           
        }

        protected void BtnEditedClick(object sender, EventArgs e)
        {
            TbxBookNumber.Enabled = false;
            TbxBookName.Enabled = true;
            TbxBookAuthor.Enabled = true;
            TbxBookPrice.Enabled = true;
            TbxBookPublished.Enabled = true;
            TbxBookPublishedTime.Enabled = true;
            TbxRemark.Enabled = true;
            DrpBookType.Enabled = true;
            BtnEdit.Visible = true;
            FileUpload1.Visible = true;
            Button1.Visible = true;
            BtnExc.Visible = true;
            BtnEdited.Visible= false;
        }

        protected void Button1Click(object sender, EventArgs e)
        {
            //获取数据
            var getName = FileUpload1.PostedFile.FileName;//获取图片名

            //上传文件
            var getLastpath = FileUploadCompant(FileUpload1);
            //获取上传文件流
            var getbyte = new byte[FileUpload1.PostedFile.ContentLength];
            var filestream = FileUpload1.PostedFile.InputStream;
            //读取数据
            filestream.Read(getbyte, 0, FileUpload1.PostedFile.ContentLength);

            //插入数据
            var pic = new Picture
            {
                PicturenName = getName,
                Picturn = getbyte,
                PictureType = FileUpload1.PostedFile.ContentType,
                PictureSize = FileUpload1.PostedFile.ContentLength,
                PicturePath = getLastpath
            };
            PictureBll.InsertBookImage(pic);
            Session["Image"] = pic;
            img.Src = "ShowImage.aspx";
        }
        /// <summary>
        /// 文件上传操作
        /// </summary>
        /// <param name="getfileupload"></param>
        /// <returns></returns>
        public string FileUploadCompant(FileUpload getfileupload)
        {
            var takeServerpath = string.Empty;   //保存到服务器路径
            try
            {
                if (string.IsNullOrEmpty(getfileupload.PostedFile.FileName))
                {
                    ScriptManager.RegisterStartupScript(Page, GetType(), "aler ", "alert( '请选择上传文件的路径!'); ", true);
                    getfileupload.Focus();
                }
                else
                {
                    string filepath = getfileupload.PostedFile.FileName;//获得全路径
                    //获得文件名
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);

                    //保存服务器路径 -唯一文件名
                    string serverpath = Server.MapPath(@"FileuploadDict/") + DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;

                    //保存到服务器上
                    getfileupload.SaveAs(serverpath);

                    //保存成功，提示信息
                    ScriptManager.RegisterStartupScript(Page, GetType(), "aler ", "alert( '文件上传成功！'); ", true);
                    takeServerpath = serverpath;
                }
            }
            catch (Exception se)
            {
                //抓取一场信息 并提示
                ScriptManager.RegisterStartupScript(Page, GetType(), "aler ", "alert( '文件上传失败-上传过程出现如下异常信息:" + se.Message.ToString(CultureInfo.InvariantCulture) + "！'); ", true);
            }
            return takeServerpath;
        }
       
    }
}